////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.data.sqlite {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	
	import kiwi.utils.Validation;
	
	import plantool.core.CoreObject;
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class SQLiteQuery extends CoreObject {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _statement:SQLStatement		= null;
		private var _onCompleteHandler:Function	= null;
		private var _onErrorHandler:Function	= null;
		private var _userdata:*					= null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function SQLiteQuery(connection:SQLConnection, q:String, onCompleteHandler:Function, onErrorHandler:Function, itemClass:Class = null, userdata:* = null) {
			super();
			
			//store the complete and error handler
			_onCompleteHandler	= onCompleteHandler;
			_onErrorHandler		= onErrorHandler;
			_userdata			= userdata;
			
			//create the sql-statement
			_statement 					= new SQLStatement();
			_statement.sqlConnection 	= connection;
			_statement.text				= q;
			_statement.itemClass		= itemClass;
			
			//listen to complete and error events
			_statement.addEventListener(SQLEvent.RESULT		, onSqlResult);
			_statement.addEventListener(SQLErrorEvent.ERROR	, onSqlError);
			
			//execute the query
			_statement.execute();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onSqlResult(result:SQLEvent):void {
			//get statement result
			var sqlResult:SQLResult 	= _statement.getResult();
			
			//create query-result to return to caller
			var queryResult:QueryResult	= new QueryResult(sqlResult.data, _statement.text, sqlResult.lastInsertRowID, _userdata);
			
			//call oncomplete handler
			if (Validation.isSet(_onCompleteHandler)) {
				_onCompleteHandler(queryResult);
			}
		}
		
		private function onSqlError(result:SQLErrorEvent):void {
			//create errorresult to return to caller
			var queryErrorResult:QueryErrorResult	= new QueryErrorResult(_statement.text, result.error.getStackTrace());
			
			//call onerror handler
			if (Validation.isSet(_onErrorHandler)) {
				_onErrorHandler(queryErrorResult);
			} else {
				trace("ERROR: Unhandled SQL Error in query:", _statement.text);
				trace(result.error.getStackTrace());
			}
		}
	}
}